package com.t.bricks.bussiness.util.wechat.subscribeaccount;

import java.util.Date;

import com.thoughtworks.xstream.XStream;

/**
 * 微信文本消息工具类
 */
public class TextMessageUtil {

	/**
	 * 将发送消息封装成对应的xml格式
	 * @param message
	 * @return
	 */
	private static String messageToxml(MessageText message) {
		XStream xstream = new XStream();
		xstream.alias("xml", message.getClass());
		return xstream.toXML(message);
	}

	/**
	 * 封装发送消息对象,封装时，需要将调换发送者和接收者的关系
	 * @param sFromUserName
	 * @param sToUserName
	 * @param sMsg
	 * @return
	 */
	public static String initMessage(String sFromUserName, String sToUserName, String sMsg) {
		MessageText text = new MessageText();
		text.setToUserName(sFromUserName);
		text.setFromUserName(sToUserName);
		text.setContent(sMsg);
		text.setCreateTime(new Date().getTime());
		text.setMsgType("text");
		
		return messageToxml(text);
	}

	/**
	 * 封装发送消息对象,封装时，需要将调换发送者和接收者的关系
	 * @param sFromUserName
	 * @param sToUserName
	 * @param msgs
	 * @return
	 */
	public static String initMessage(String sFromUserName, String sToUserName, String...msgs) {
		if (null == msgs || msgs.length < 1) {
			return initMessage(sFromUserName, sToUserName, "");
		}

		StringBuilder sb = new StringBuilder();
		for (String str : msgs) {
			sb.append(str);
		}
		
		return initMessage(sFromUserName, sToUserName, sb.toString());
	}

}
